package org.software.views.classes;
import org.software.controller.ClassController;
import org.software.entity.Class;
import org.software.views.MenuFrame;
import org.software.service.ClassService;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class ClassListFrame extends JFrame{

private JTable classTable;
private ClassController classController = new ClassController();
private ClassService classService;
private String className;

    public ClassListFrame() {
        setTitle("班级列表");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        JPanel buttonPanel = new JPanel();
        JButton updateButton = new JButton("修改");
        JButton restartButton = new JButton("重置");
        JButton returnButton = new JButton("返回");
        JButton deleteButton = new JButton("删除");

        String[] columnNames = {"班级ID", "班级名称", "小组数量"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);

        List<Class> aClasses = new ClassService().classQuery();
        for (Class clazz : aClasses) {
            Object[] row = {clazz.getClassId(), clazz.getClassName(), clazz.getGroupCount()};
            tableModel.addRow(row);
        }
        classTable = new JTable(tableModel);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        classTable.setDefaultRenderer(Object.class, tcr);
        classTable.setRowHeight(30);
        classTable.setRowSelectionAllowed(true);
        classTable.setColumnSelectionAllowed(false);
        classTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = classTable.getSelectedRow();
                if(row>=0){
                    className = (String) classTable.getValueAt(row,1);
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(classTable){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 400);
            }
        };



        updateButton.addActionListener(e -> {
            if(className==null){
                JOptionPane.showMessageDialog(null,"请选择班级");
            }else{
                new ClassUpdateFrame(className);
                dispose();
            }
        });
        deleteButton.addActionListener(e -> {
            if(className==null){
                JOptionPane.showMessageDialog(null,"请选择班级");
            }else{
                int result = JOptionPane.showConfirmDialog(null,"确定删除吗？","提示",JOptionPane.YES_NO_OPTION);
                if(result==JOptionPane.YES_OPTION){
                    if(classController.classDelete(className)){
                        JOptionPane.showMessageDialog(null,"删除成功");
                        tableModel.fireTableDataChanged();
                        classTable.updateUI();
                    }
                }
            }
        });
        returnButton.addActionListener(e -> {

            new MenuFrame();
            dispose();
        });
        buttonPanel.add(updateButton);
        buttonPanel.add(restartButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(returnButton);
        panel.add(buttonPanel);
        panel.add(scrollPane);
        add(panel);


        setVisible(true);
    }
}

